package client.ui.component.edittext;

import client.ui.util.MaterialColors;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
/**
  * @Description 文字输入框
  * @author 孔明浩
  * @Date 2020/3/31
  */
public class TextEdit extends JPanel {
    protected final Color errorColor = MaterialColors.COSMO_RED;
    protected final Color focusColor = MaterialColors.COSMO_BLUE;
    protected final Color normalColor = MaterialColors.COSMO_DARK_GRAY;
    protected int hintTextSize = 12;
    protected int column = 22;
    //绘制时的颜色，和计时器结合使用
    protected Color paintColor;
    //行尾图标
    protected ImageIcon endImage;
    protected boolean focused;
    //提示文字
    protected String hintText;
    //出错文字，不为空则改变输入框颜色
    protected String errorText;
    //文字输入框本体
    protected JTextField textField;
    //行尾图标的容器，可以增加监听
    protected JLabel endLabel;
    //动画计数
    protected int num = 0;
    //动画分割数量
    protected int max = 10;
    protected Timer timer;
    public TextEdit() {
        this("");
    }
    public TextEdit(String hint){
        super();
        this.hintText = hint;
        initial();
    }

    protected void initial(){
        if (this.textField == null){
            this.textField = new JTextField();
        }
        this.setLayout(new FlowLayout(FlowLayout.LEFT));
        this.add(textField);
        this.textField.setColumns(column);
        this.textField.setBackground(null);
        this.textField.setBorder(null);
        this.textField.setFont(new Font("宋体",Font.BOLD,(int)(hintTextSize * 1.5f)));
        this.setBorder(new EmptyBorder(hintTextSize + 1,0,hintTextSize,0));
        //设置监听，在获取焦点之后重绘颜色
        this.textField.addFocusListener(new FocusListener() {
            @Override
            public void focusGained(FocusEvent e) {
                TextEdit.this.focused = true;
                if (timer != null && timer.isRunning()){
                    timer.stop();
                }
                timer = new Timer(20,new GetFocusedListener());
                timer.start();
            }

            @Override
            public void focusLost(FocusEvent e) {
                TextEdit.this.focused = false;
                if (timer != null && timer.isRunning()){
                    timer.stop();
                }
                timer = new Timer(10,new LoseFocusListener());
                timer.start();
            }
        });
        //绘制行尾图标
        endLabel = new JLabel(endImage);
        this.add(endLabel);
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        Graphics2D g2=(Graphics2D) g;
        int h = this.getHeight();
        int w = this.textField.getWidth();
        //设置绘图质量
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
        //绘制下划线
        if (errorText != null && errorText.length() > 0){
            paintColor = errorColor;
        } else if (num == max){
            paintColor = focusColor;
        } else if (num == 0){
            paintColor = normalColor;
        }
        g2.setColor(paintColor);
        g2.setStroke(new BasicStroke(1.5f));
        g2.drawLine(0,h- hintTextSize - 1,w + 36,h - hintTextSize - 1);
        //下划线绘制结束
        Paint textPaint;
        //绘制错误信息（不为空时）
        if (errorText != null && errorText.length() > 0){
            textPaint = new GradientPaint(0,0,errorColor,0,0,errorColor);
            g2.setPaint(textPaint);
            Font font = new Font("微软雅黑",Font.PLAIN,hintTextSize - 1);
            g2.setFont(font);
            g2.setStroke(new BasicStroke(1));
            g2.drawString(errorText,0,h - 2);
        }
        //绘制提示
        if (hintText != null && hintText.length() > 0){
            if (focused){
                textPaint = new GradientPaint(0,0,focusColor,0,0,focusColor);
            } else if (textField.getText() != null && textField.getText().length() > 0){
                textPaint = new GradientPaint(0,0,MaterialColors.GRAY_700,0,0,MaterialColors.GRAY_700);
            } else {
                textPaint = new GradientPaint(0,0,normalColor,0,0,normalColor);
            } if (errorText != null && errorText.length() > 0){
                textPaint = new GradientPaint(0,0,errorColor,0,0,errorColor);
            }
            Font font = new Font("微软雅黑",Font.PLAIN,hintTextSize);
            g2.setPaint(textPaint);
            g2.setFont(font);
            g2.setStroke(new BasicStroke(1));
            g2.drawString(hintText,0,hintTextSize);
        }

        g2.dispose();
    }

    //设置初始文本
    public void setText(String text){
        this.textField.setText(text);
        repaint();
    }

    public String getText(){
        return this.textField.getText();
    }

    public String getHintText() {
        return hintText;
    }

    public void setHintText(String hintText) {
        this.hintText = hintText;
        repaint();
    }

    public String getErrorText() {
        return errorText;
    }

    /**
     * @Description 设置错误信息，设为空或者""则不显示
     * @param errorText 出错时的文本
     * @return void
     * @author 孔明浩
     * @Date 2020/3/31
     */
    public void setErrorText(String errorText) {
        this.errorText = errorText;
        repaint();
    }

    public int getColumn() {
        return column;
    }

    public void setColumn(int column) {
        this.column = column;
        this.textField.setColumns(column);
        repaint();
    }

    public ImageIcon getEndImage() {
        return endImage;
    }

    public void setEndImage(ImageIcon endImage) {
        this.endImage = endImage;
        this.endLabel.setIcon(this.endImage);
        repaint();
    }

    public JLabel getEndLabel() {
        return endLabel;
    }

    public JTextField getTextField() {
        return textField;
    }

    //获取焦点后的行为
    protected class GetFocusedListener implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e) {
            //绘制随时间变化的颜色
            double individualR = ( 1.0 * focusColor.getRed() - normalColor.getRed()) / max;
            double individualG = ( 1.0 * focusColor.getGreen() - normalColor.getGreen()) / max;
            double individualB = ( 1.0 * focusColor.getBlue() - normalColor.getBlue()) / max;
            num++;
            if (num <= max){
                paintColor = new Color(normalColor.getRed() + (int)individualR * num,
                        normalColor.getGreen() + (int)individualG * num,
                        normalColor.getBlue() + (int)individualB * num
                        );
            } else {
                timer.stop();
            }
            repaint();
        }
    }

    //失去焦点后的行为
    protected class LoseFocusListener implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {
            //绘制随时间变化的颜色
            double individualR = ( 1.0 * focusColor.getRed() - normalColor.getRed()) / max;
            double individualG = ( 1.0 * focusColor.getGreen() - normalColor.getGreen()) / max;
            double individualB = ( 1.0 * focusColor.getBlue() - normalColor.getBlue()) / max;
            num--;
            if (num >= 0){
                paintColor = new Color(normalColor.getRed() + (int)individualR * num,
                        normalColor.getGreen() + (int)individualG * num,
                        normalColor.getBlue() + (int)individualB * num
                );
            } else {
                timer.stop();
            }
            repaint();
        }
    }
}
